<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的注册信息
$account = $_POST['账号'] ?? '';
$password = $_POST['密码'] ?? '';
$nickname = $_POST['昵称'] ?? '';
$avatar = $_POST['头像'] ?? '';
$qq = $_POST['QQ'] ?? '';
$email = $_POST['邮箱'] ?? '';
$gender = $_POST['性别'] ?? '';
$birthday = $_POST['生日'] ?? '';
$identity = $_POST['身份'] ?? '';
$ban = $_POST['封禁'] ?? '假';
$ban_reason = $_POST['封禁原因'] ?? '';
$balance = $_POST['余额'] ?? '';
$sign_in_count = $_POST['签到次数'] ?? '';
$sign_in_date = $_POST['签到日期'] ?? '';
$file_upload_count = $_POST['上传文件数量'] ?? '';
$post_count = $_POST['发布数量'] ?? '';
$message_record = $_POST['消息记录'] ?? '';
$qq_interconnect = $_POST['QQ互联'] ?? '';
$signature = $_POST['签名'] ?? '';
$intro = $_POST['介绍'] ?? '';
$note = $_POST['备注'] ?? '';
$avatar_frame = $_POST['头像框'] ?? '';
$tarot_owned = $_POST['已拥有塔罗'] ?? '';
$tarot_used = $_POST['使用塔罗'] ?? '';
$dice_owned = $_POST['已拥有骰子'] ?? '';
$dice_used = $_POST['使用骰子'] ?? '';
$province = $_POST['地址省'] ?? '';
$city = $_POST['地址市'] ?? '';
$county = $_POST['地址县'] ?? '';
$town = $_POST['地址镇'] ?? '';
$detailed_address = $_POST['详细地址'] ?? '';
$member_time = $_POST['会员时间'] ?? '';
$is_member = $_POST['是否会员'] ?? '假';
$video_upload_count = $_POST['上传视频数量'] ?? '';

// 检查账号和密码是否为空
if (empty($account) || empty($password)) {
    $result = [
        'status' => 'error',
        'message' => '账号和密码不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 检查用户是否存在
    $checkUserStmt = $pdo->prepare("SELECT id FROM mi_users WHERE 账号 = :account");
    $checkUserStmt->bindParam(':account', $account);
    $checkUserStmt->execute();
    $userExists = $checkUserStmt->fetch(PDO::FETCH_ASSOC);

    if ($userExists) {
        // 用户已存在
        $result = [
            'status' => 'error',
            'message' => '用户已存在'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 用户不存在，进行注册
    $insertUserStmt = $pdo->prepare("INSERT INTO mi_users (
                账号, 密码, 昵称, 头像, QQ, 邮箱, 性别, 生日, 身份, 封禁, 
                封禁原因, 余额, 签到次数, 签到日期, 上传文件数量, 发布数量, 
                消息记录, QQ互联, 签名, 介绍, 备注, 头像框, 已拥有塔罗, 
                使用塔罗, 已拥有骰子, 使用骰子, 地址省, 地址市, 地址县, 
                地址镇, 详细地址, 会员时间, 是否会员, 上传视频数量
            ) VALUES (
                :account, :password, :nickname, :avatar, :qq, :email, :gender, :birthday, :identity, :ban, 
                :ban_reason, :balance, :sign_in_count, :sign_in_date, :file_upload_count, :post_count, 
                :message_record, :qq_interconnect, :signature, :intro, :note, :avatar_frame, :tarot_owned, 
                :tarot_used, :dice_owned, :dice_used, :province, :city, :county, :town, 
                :detailed_address, :member_time, :is_member, :video_upload_count
            )");

    // 绑定参数
    $insertUserStmt->bindParam(':account', $account);
    $insertUserStmt->bindParam(':password', $password);
    $insertUserStmt->bindParam(':nickname', $nickname);
    $insertUserStmt->bindParam(':avatar', $avatar);
    $insertUserStmt->bindParam(':qq', $qq);
    $insertUserStmt->bindParam(':email', $email);
    $insertUserStmt->bindParam(':gender', $gender);
    $insertUserStmt->bindParam(':birthday', $birthday);
    $insertUserStmt->bindParam(':identity', $identity);
    $insertUserStmt->bindParam(':ban', $ban);
    $insertUserStmt->bindParam(':ban_reason', $ban_reason);
    $insertUserStmt->bindParam(':balance', $balance);
    $insertUserStmt->bindParam(':sign_in_count', $sign_in_count);
    $insertUserStmt->bindParam(':sign_in_date', $sign_in_date);
    $insertUserStmt->bindParam(':file_upload_count', $file_upload_count);
    $insertUserStmt->bindParam(':post_count', $post_count);
    $insertUserStmt->bindParam(':message_record', $message_record);
    $insertUserStmt->bindParam(':qq_interconnect', $qq_interconnect);
    $insertUserStmt->bindParam(':signature', $signature);
    $insertUserStmt->bindParam(':intro', $intro);
    $insertUserStmt->bindParam(':note', $note);
    $insertUserStmt->bindParam(':avatar_frame', $avatar_frame);
    $insertUserStmt->bindParam(':tarot_owned', $tarot_owned);
    $insertUserStmt->bindParam(':tarot_used', $tarot_used);
    $insertUserStmt->bindParam(':dice_owned', $dice_owned);
    $insertUserStmt->bindParam(':dice_used', $dice_used);
    $insertUserStmt->bindParam(':province', $province);
    $insertUserStmt->bindParam(':city', $city);
    $insertUserStmt->bindParam(':county', $county);
    $insertUserStmt->bindParam(':town', $town);
    $insertUserStmt->bindParam(':detailed_address', $detailed_address);
    $insertUserStmt->bindParam(':member_time', $member_time);
    $insertUserStmt->bindParam(':is_member', $is_member);
    $insertUserStmt->bindParam(':video_upload_count', $video_upload_count);

    // 执行插入操作
    if ($insertUserStmt->execute()) {
        // 注册成功
        $result = [
            'status' => 'success',
            'message' => '注册成功'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    } else {
        // 获取失败信息
        $errorInfo = $insertUserStmt->errorInfo();
        $result = [
            'status' => 'error',
            'message' => '注册失败：' . $errorInfo[2]
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
